package awk;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;

import util.Log4jUtil;

public abstract class LineHandler {
    protected static Logger logger = Logger.getLogger(LineHandler.class) ;
    protected int defaultIntValue = 0 ;

    public static String FS=";" ;
    protected String RS ="\n" ;

    protected String OFS = "," ;
    protected String ORS = "\n" ;
    
    protected String $0 ="" ;
    protected String [] $ ;
    
    protected int NF =0 ;
    protected int NR =0 ;
    protected int FNR =0 ;
    
    protected String FILENAME ="" ;
    
    public void setLog(String file){
        FileAppender fp = Log4jUtil.getFileAppender(file) ;
        logger.addAppender(fp) ;
        
    }
    public void info(String message){
        logger.info(message) ;
    }
    public void error(String message){
        logger.error(message ) ;
    }
    public int getInt(String i){
        try{
            return Integer.parseInt(i) ; 
        }catch(NumberFormatException e){
            logger.error("error when parse " + i +" to int", e) ;
            return defaultIntValue ;
        }
    }
    public long getLong(String i){
        try{
            return Long.parseLong(i) ; 
        }catch(NumberFormatException e){
            logger.error("error when parse " + i +" to long", e) ;
            return defaultIntValue ;
        }
    }
    public double getDouble(String i){
        try{
            return Double.parseDouble(i) ; 
        }catch(NumberFormatException e){
            logger.error("error when parse " + i +" to double", e) ;
            return defaultIntValue ;
        }
    }
    public void handleFile(File file) throws IOException{
        logger.info("...begin to split the file to records, and handle records...") ;
        StringBuffer context = new StringBuffer(getFileContext(file) );
        int len=RS.length() ;
        int begin=0 ;
        int i=0;
        while( begin >=0){
            String record = "" ;
            int next = context.indexOf(RS, begin+len ) ;
            if( next >=0 ){
                record = context.substring(begin + len, next) ;
                begin = next+len ;
            }
            record = context.substring(begin + len) ;
            begin = next ;
            ++i;
          System.out.println(i) ;
            $0=record ;
            $=record.split(FS) ;
            body($) ;
            
        }
        logger.info("...end split the file to records...") ;

    }
    public void handleFileByLine(File file) throws IOException{
        logger.info("...begin to get file context...") ;

        BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();

        String line="";
        int i=0;
        while ((line = in.readLine()) != null) {
            ++i ;
            $0=line ;
            $=line.split(FS) ;
            try{
                body($) ;
            }catch(Exception e){
                logger.error("Exception happen at line of: \n\t[" + line +"]\n" +
                		e.toString(), e) ;
            }
            sb.append(line);
        }
    }
    protected String getFileContext(File file) throws IOException{
        logger.info("...begin to get file context...") ;

        BufferedReader in = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();

        String line="";
        int i=0;
        while ((line = in.readLine()) != null) {
            if( i!=0 ) line="\n" +line ;
            ++i ;
            sb.append(line);
        }
//        System.out.println(sb.toString()) ;
        logger.info("...end get file context...") ;

        return sb.toString() ;
    }
    public void handleFile(String file) throws IOException{
       handleFile(new File(file)) ;
    }
    public abstract void begin();
    public abstract void body(String[] args) ;
    public abstract void end();
 
}
